function feature_matrix = PreparePanFeatures(start_ind, num_points, max_window_width, window_increment, derivative_range, lecturer, saliency, boards, board_scale_factor, faceDetection) 

lecturer_turn_around_range = 1500;

% Number of features.
num_features = 1;
for ind = 100:window_increment:max_window_width
    num_features = num_features + 1;
end

% for ind = max_window_width:window_increment:1.5*max_window_width
%     num_features = num_features + 1;
% end

for ind = derivative_range
    num_features = num_features + 1;
end

for ind = lecturer_turn_around_range
    num_features = num_features + 1;
end

% Extract board centers
board_center_x = (boards(:,1) + boards(:,2))/2;
board_center_x = board_center_x*board_scale_factor;

% Extract saliency - x coordinate
saliency_train = saliency(start_ind:(start_ind-1)+num_points,2);

%initailize feature index
feature_ind = 1;

feature_matrix = zeros(num_features, num_points);
num_boards = size(board_center_x,1);

% %mean distance compared to board
%       for i=1:num_points
%        feature_matrix(feature_ind,i) = abs(saliency_train(i)-mean(saliency_train(max(1,i-30):min(num_points,i+30)))); 
%       end
%        feature_ind = feature_ind +1;

% x-distance of saliency to the closest board center
for i=1:num_points
    dist = abs(saliency_train(i)*ones(num_boards,1)-board_center_x); %distance to different boards
    feature_matrix(feature_ind,i) = min(dist); % put min distance into feature matrix
end

% Saliency derivative with respect to time
for ind = derivative_range
    feature_ind = feature_ind + 1;
    for i=1:num_points
        feature_matrix(feature_ind,i) = abs(saliency_train(max(1,i-ind)) - saliency_train(min(num_points,i+ind))); 
    end
 end

%current position compared to mean of the window
 for ind = 100:window_increment:max_window_width
    feature_ind = feature_ind + 1;
    for i=1:num_points
        feature_matrix(feature_ind,i) = abs(saliency_train(i)-mean(saliency_train(max(1,i-ind):min(num_points,i+ind)))); 
    end
 end
 
%  feature_ind = feature_ind + 1;
%  for i=1:num_points
%      feature_matrix(feature_ind,i) = faceDetection(i);
%  end
 
 % Closest time in the future when the lecturer comes back < 180 frames ==
 % 6 seconds
 lecturerX = smooth(lecturer(:,2),100);
 %tic
 for frames_ahead = lecturer_turn_around_range
     i = 1;
     feature_ind = feature_ind + 1;
     while i<num_points
        return_counter = 60;
        while (lecturerX(i) ~= lecturerX(i+return_counter) && i + return_counter < num_points && return_counter < frames_ahead)
            return_counter = return_counter + 1;
        end

        if (return_counter >= frames_ahead || i + return_counter >= num_points)
            feature_matrix(feature_ind,i) = 1;
            i = i + 1;
        else
            feature_matrix(feature_ind,i:i+return_counter) = -1;
            i = i + return_counter + 1;
        end
     end
 end
 %toc
 
 
 
% Saliency variance
%  for ind = 100:window_increment:max_window_width
%     feature_ind = feature_ind + 1;
%     for i=1:num_points
%         feature_matrix(feature_ind,i) = var(saliency_train(max(1,i-ind):min(num_points,i+ind))); 
%     end
%  end
 
 %Saliency range over the window
%  for ind = max_window_width:window_increment:1.5*max_window_width
%     feature_ind = feature_ind + 1;
%     for i=1:num_points
%         this_vector = saliency_train(max(1,i-ind):min(num_points,i+ind));
%         feature_matrix(feature_ind,i) = max(this_vector) - min(this_vector); 
%     end
%  end
 
 
%  deviation_feature_ind = feature_ind-1;
%  
%  %past information lecturer tracking
% for ind = 1:1:30 
% % feature_matrix(feature_ind,1:ind-1) = feature_matrix(1,1:ind-1);
%  feature_matrix(feature_ind,ind:num_points) = feature_matrix(deviation_feature_ind,1:num_points-ind+1); 
%  %   feature_matrix(feature_ind,:)= abs(feature_matrix(feature_ind,:) -feature_matrix(1,:));
% feature_ind = feature_ind +1;
% end

%future information lecturer tracking
% for ind = 1:1:30  
% %feature_matrix(feature_ind,num_points-ind+2:num_points) = feature_matrix(1,num_points-ind+2:num_points);
% feature_matrix(feature_ind,1:num_points-ind+1) = feature_matrix(1,ind:num_points);
% %feature_matrix(feature_ind,:)= abs(feature_matrix(feature_ind,:) -feature_matrix(1,:));
% feature_ind = feature_ind +1;
% end
 % feature_matrix(feature_ind,:) = lecturer(start_ind:(start_ind-1)+num_points,2)';
% feature_ind = feature_ind+1;
% feature_matrix(feature_ind,:) = saliency(start_ind:(start_ind-1)+num_points,2)';
% feature_ind = feature_ind+1;

%==========================================================================

 
%  for ind = 1:window_increment:max_window_width 
%     for i=1:num_points
%      feature_matrix(feature_ind,i) = abs(lecturer_train(i)-mean(feature_matrix(2,max(1,i-ind):min(num_points,i+ind)))); 
%     end
%      feature_ind = feature_ind +1;
%  end



%past information lecturer tracking
% for ind = 1:window_increment:max_window_width 
%    for i=1:num_points
%     feature_matrix(feature_ind,num_points) = abs(feature_matrix(feature_ind,num_points)-mean(feature_matrix(1,max(1,i-ind):min(num_points,i+ind)))); 
%    end
%     feature_ind = feature_ind +1;
% end
% 
% for ind = 1:window_increment:max_window_width 
%    for i=1:num_points
%     feature_matrix(feature_ind,num_points) = abs(feature_matrix(feature_ind,num_points)-mean(feature_matrix(2,max(1,i-ind):min(num_points,i+ind)))); 
%    end
%     feature_ind = feature_ind +1;
% end

%==========================================================================

% 
% %past information lecturer tracking
% for ind = 1:window_increment:max_window_width 
% % feature_matrix(feature_ind,1:ind-1) = feature_matrix(1,1:ind-1);
%  feature_matrix(feature_ind,ind:num_points) = feature_matrix(1,1:num_points-ind+1); 
%  %   feature_matrix(feature_ind,:)= abs(feature_matrix(feature_ind,:) -feature_matrix(1,:));
% feature_ind = feature_ind +1;
% end
% 
% %future information lecturer tracking
% for ind = 1:window_increment:max_window_width 
% %feature_matrix(feature_ind,num_points-ind+2:num_points) = feature_matrix(1,num_points-ind+2:num_points);
% feature_matrix(feature_ind,1:num_points-ind+1) = feature_matrix(1,ind:num_points);
% %feature_matrix(feature_ind,:)= abs(feature_matrix(feature_ind,:) -feature_matrix(1,:));
% feature_ind = feature_ind +1;
% end
% 
% %past information saliency
% for ind = 1:window_increment:max_window_width 
% % feature_matrix(feature_ind,1:ind-1) = feature_matrix(2,1:ind-1);
%  feature_matrix(feature_ind,ind:num_points) = feature_matrix(2,1:num_points-ind+1);
% % feature_matrix(feature_ind,:)= abs( feature_matrix(feature_ind,:) -feature_matrix(2,:));
% feature_ind = feature_ind +1;
% end
% 
% %future information saliency
% for ind = 1:window_increment:max_window_width 
% % feature_matrix(feature_ind,num_points-ind+2:num_points) = feature_matrix(2,num_points-ind+2:num_points);
% feature_matrix(feature_ind,1:num_points-ind+1) = feature_matrix(2,ind:num_points );
% % feature_matrix(feature_ind,:)= abs(feature_matrix(feature_ind,:) -feature_matrix(2,:));
% feature_ind = feature_ind +1;
% end

%normalize features to between 0 and 1 for LIBSVM
for feature_ind = 1 : num_features
    if(max(feature_matrix(feature_ind,:)) ~= 0)
        feature_matrix(feature_ind,:)= feature_matrix(feature_ind,:)./max(feature_matrix(feature_ind,:));    
    else
         feature_matrix(feature_ind,:)=0;    
    end
end
